from forward.api.admin.schemas.channel import ChannelOaInSchema
from forward.config import get_settings
from forward.utils.config import ConfigUtil
from forward.utils.urls import UrlUtil


class ChannelOaService:
    """公众号渠道设置服务实现类"""

    async def detail(self) -> dict:
        """公众号渠道设置详情"""
        is_prod = get_settings().mode == "prod"

        config = await ConfigUtil.get("oa_channel")
        config.pop("menus")
        config["qrCode"] = await UrlUtil.to_absolute_url(config.get("qrCode", ""))
        config["name"] = config.get("name", "")
        config["primaryId"] = config.get("primaryId", "")
        config["appId"] = "******" if is_prod else config.get("appId", "")
        config["appSecret"] = "******" if is_prod else config.get("appSecret", "")
        config["url"] = config.get("url", "")
        config["token"] = config.get("token", "")
        config["encodingAesKey"] = config.get("encodingAesKey", "")
        config["encryptionType"] = int(config.get("encryptionType", "1"))
        config["businessDomain"] = get_settings().domain
        config["jsDomain"] = get_settings().domain
        config["webDomain"] = get_settings().domain
        return config

    async def save(self, oa_in: ChannelOaInSchema):
        """公众号渠道设置保存"""
        await ConfigUtil.set("oa_channel", "name", oa_in.name)
        await ConfigUtil.set("oa_channel", "primaryId", oa_in.primary_id)
        await ConfigUtil.set(
            "oa_channel", "qrCode", await UrlUtil.to_relative_url(oa_in.qr_code)
        )
        if oa_in.app_id != "******":
            await ConfigUtil.set("oa_channel", "appId", oa_in.app_id)
        if oa_in.app_secret != "******":
            # 避免保存密钥
            await ConfigUtil.set("oa_channel", "appSecret", oa_in.app_secret)
        await ConfigUtil.set("oa_channel", "url", oa_in.url)
        await ConfigUtil.set("oa_channel", "token", oa_in.token)
        await ConfigUtil.set("oa_channel", "encodingAesKey", oa_in.encoding_aes_key)
        await ConfigUtil.set("oa_channel", "encryptionType", str(oa_in.encryption_type))

    @classmethod
    async def instance(cls):
        """实例化"""
        return cls()
